package com.oocl.shopping.controller;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oocl.shopping.dto.PayBooksDTO;
import com.oocl.shopping.pojo.Customer;
import com.oocl.shopping.repository.OrdersDB;
import com.oocl.shopping.service.CustomerService;
import com.oocl.shopping.service.impl.CustomerServiceImpl;

/**
 * Servlet implementation class AddToCartServlet
 */
public class AddToCartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private CustomerService service;   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddToCartServlet() {
    	service = new CustomerServiceImpl();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String bookId = request.getParameter("bookId");
		String bookName = request.getParameter("bookName");
		
		Map<String, PayBooksDTO> orders = OrdersDB.getOrdersMap();
		int total = 0;
		try{
			total = OrdersDB.getOrdersMap().get(bookId).getTotal();
			OrdersDB.getOrdersMap().get(bookId).setTotal(++total);
		}catch(Exception e){
			OrdersDB.getOrdersMap().put(bookId, new PayBooksDTO(bookName, 1, new Date()));
		}

		request.getRequestDispatcher("../view/showbooks.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
